package com.lemon.smartsafety.ui.adapter.common;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.common.EvidenceItemVo;
import com.lemon.smartsafety.entity.event.evidence.EvidenceDeleteEvent;
import com.lemon.smartsafety.entity.event.evidence.EvidenceItemEvent;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;

import cn.finalteam.galleryfinal.model.PhotoInfo;
import cn.finalteam.galleryfinal.widget.GFImageView;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/25.
 * 添加
 */
public class SelectEvidencePhotoItem implements AdapterItem<EvidenceItemVo<PhotoInfo>> {
    private LoggerHelper loggerHelper = new LoggerHelper(SelectEvidencePhotoItem.class);

    private LinearLayout ripple;

    private GFImageView mGFImageView;
    private ImageView mDeleteIcon;
    private Context mContext;
    private int mHeight = 100;
    private boolean canDelete = true;
    private int mPosition = -1;
    private EvidenceItemVo<PhotoInfo> mItem;

    public SelectEvidencePhotoItem() {
    }

    public SelectEvidencePhotoItem(boolean delete) {
        this.canDelete = delete;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.item_evidence_photo;
    }

    @Override
    public void bindViews(View root) {
        mContext = root.getContext();
        mHeight = (int) mContext.getResources().getDimension(R.dimen.evidence_item_size);
        ripple = (LinearLayout) root.findViewById(R.id.ripple);
        mGFImageView = (GFImageView) root.findViewById(R.id.ivPhoto);
        mDeleteIcon = (ImageView) root.findViewById(R.id.ivDelete);
        if (this.canDelete) {
            mDeleteIcon.setVisibility(View.VISIBLE);
        } else {
            mDeleteIcon.setVisibility(View.GONE);
        }
    }

    @Override
    public void setViews() {
        mDeleteIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPosition == -1) {
                    return;
                }
                EvidenceDeleteEvent event = new EvidenceDeleteEvent();
                event.setItem(mItem);
                AppService.getBus().post(event);
            }
        });
        mGFImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EvidenceItemEvent event = new EvidenceItemEvent();
                event.setTag(EventTag.TAG_EVIDENCE_ITEM);
                event.setUrl(mItem.getData().getPhotoPath());
                event.setType(mItem.getViewType());
                AppService.getBus().post(event);
            }
        });
    }

    @Override
    public void handleData(EvidenceItemVo<PhotoInfo> evidenceItemVo, int position) {
        mPosition = position;
        mItem = evidenceItemVo;
        if (mItem.getViewType() == RcvViewType.EvidenceViewType.TYPE_VOICE) {
            mGFImageView.setTag(ImageViewTag.TAG_VOICE);
            //mGFImageView.setImageResource(R.mipmap.ic_settings_voice);
            mGFImageView.setImageDrawable(ContextCompat.getDrawable(IApplication.context(), R.mipmap.icon_new_voice));

        } else if (mItem.getViewType() == RcvViewType.EvidenceViewType.TYPE_PHOTO) {
            mGFImageView.setTag(ImageViewTag.TAG_PHOTO);
            GlideImageLoader.loadImage(mContext, evidenceItemVo.getData()
                    .getPhotoPath(), mGFImageView, mHeight, mHeight);
        }
    }


    public enum ImageViewTag {
        TAG_VOICE, TAG_PHOTO;
    }
}
